package com.ruoyi.web.controller.module.event;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.event.EventMaintenanceContract;
import com.ruoyi.system.service.module.event.IEventMaintenanceContractService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 维保合同列Controller
 *
 * @author ruoyi
 * @date 2023-12-26
 */
@Validated
@Api(value = "维保合同列控制器", tags = {"维保合同列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/maintenance/contract")
public class EventMaintenanceContractController extends BaseController {

    private final IEventMaintenanceContractService eventMaintenanceContractService;

    /**
     * 查询维保合同列列表
     */
    @ApiOperation("查询维保合同列列表")
    @PreAuthorize("@ss.hasPermi('module:maintenanceContract:list')")
    @GetMapping("/list")
    public TableDataInfo<EventMaintenanceContract> list(@Validated EventMaintenanceContract eventMaintenanceContract) {
        QueryWrapper<EventMaintenanceContract> qw = new QueryWrapper<>();
		if (eventMaintenanceContract.getMaintenanceId()!=null){
			qw.eq("maintenance_id",eventMaintenanceContract.getMaintenanceId());
		}
        TableDataInfo<EventMaintenanceContract> td = PageUtils.buildDataInfo(eventMaintenanceContractService.page(PageUtils.buildPage(),qw));
        List<EventMaintenanceContract> list = td.getRows();
        return td;
    }

    /**
     * 导出维保合同列列表
     */
    @ApiOperation("导出维保合同列列表")
    @PreAuthorize("@ss.hasPermi('module:maintenanceContract:export')")
    @Log(title = "维保合同列", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EventMaintenanceContract> export(@Validated EventMaintenanceContract eventMaintenanceContract) {
        List<EventMaintenanceContract> list = eventMaintenanceContractService.list();
        ExcelUtil<EventMaintenanceContract> util = new ExcelUtil<EventMaintenanceContract>(EventMaintenanceContract.class);
        return util.exportExcel(list, "维保合同列");
    }

    /**
     * 获取维保合同列详细信息
     */
    @ApiOperation("获取维保合同列详细信息")
    @PreAuthorize("@ss.hasPermi('module:maintenanceContract:query')")
    @GetMapping("/{id}")
    public AjaxResult<EventMaintenanceContract> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
        EventMaintenanceContract eventMaintenanceContract = eventMaintenanceContractService.getById(id);
		if (eventMaintenanceContract.getServiceBeginTime()!=null&&eventMaintenanceContract.getServiceEndTime()!=null){
			List<String> time = new ArrayList<>();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(eventMaintenanceContract.getServiceBeginTime());
			String beginTime = calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+1+"-"+calendar.get(Calendar.DATE);
			calendar.setTime(eventMaintenanceContract.getServiceEndTime());
			String endTime = calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+1+"-"+calendar.get(Calendar.DATE);
			time.add(beginTime);
			time.add(endTime);
			eventMaintenanceContract.setDateRange(time);
		}
        return AjaxResult.success(eventMaintenanceContract);
    }

    /**
     * 新增维保合同列
     */
    @ApiOperation("新增维保合同列")
    @PreAuthorize("@ss.hasPermi('module:maintenanceContract:add')")
    @Log(title = "维保合同列", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EventMaintenanceContract eventMaintenanceContract) {

        return toAjax(eventMaintenanceContractService.save(eventMaintenanceContract) ? 1 : 0);
    }

    /**
     * 修改维保合同列
     */
    @ApiOperation("修改维保合同列")
    @PreAuthorize("@ss.hasPermi('module:maintenanceContract:edit')")
    @Log(title = "维保合同列", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EventMaintenanceContract eventMaintenanceContract) {
        return toAjax(eventMaintenanceContractService.updateById(eventMaintenanceContract) ? 1 : 0);
    }

    /**
     * 删除维保合同列
     */
    @ApiOperation("删除维保合同列")
    @PreAuthorize("@ss.hasPermi('module:maintenanceContract:remove')")
    @Log(title = "维保合同列" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(eventMaintenanceContractService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
